import redis
import pymongo
import json

# 连接到 Redis
redis_client = redis.StrictRedis(host='redis', port=6379, db=0, decode_responses=True)

# 连接到 MongoDB 1
mongo_client1 = pymongo.MongoClient('mongodb://root:Wl728125282@192.168.2.7:7001/')
db1 = mongo_client1['mbirecord']  # MongoDB 数据库
collection1 = db1['beqege']  # MongoDB 集合

# 连接到 MongoDB 2
mongo_client2 = pymongo.MongoClient('mongodb://root:Wl728125282@mongo-service:27017/')
db2 = mongo_client2['mbikeyword']  # MongoDB 数据库
collection2 = db2['keyword']  # MongoDB 集合
collection2.create_index([('content', 1), ('type', 1)], unique=True)


# 执行转移操作
if __name__ == '__main__':
    distinct_titles = collection1.distinct('title')
    documents_to_insert = [{"content": title, "type": "title"} for title in distinct_titles]
    collection2.insert_many(documents_to_insert, ordered=False)
    print(f"集合中的文档数量: {len(distinct_titles)}")
    
    distinct_authors = collection1.distinct('author')
    documents_to_insert = [{"content": author, "type": "author"} for author in distinct_authors]
    collection2.insert_many(documents_to_insert, ordered=False)

    print(f"集合中的文档数量: {len(distinct_authors)}")
